package com.songtech.server.frame.chain.handler;

import com.songtech.server.Jedis.JedisBaseDao;
import com.songtech.server.constant.KeysConstant;
import com.songtech.server.constant.enumConstant.AlertEnum;
import com.songtech.server.entry.AlertEntry;
import com.songtech.server.entry.PositionProtocal;
import com.songtech.server.utils.CalculationUtils;
import com.songtech.server.utils.GetKeysUtils;

import java.io.Serializable;

/**
 * Create By YINN on 2018/4/26 17:41
 * Description : 通用字段告警
 */
public class EquStatusHandler implements BaseChainHandler {

    //0为确认(协议使用的反逻辑)
    private final static String ZERO = "0";

    private final static String ALERT = KeysConstant.ALERT;

    private PositionProtocal positionProtocal;

    private JedisBaseDao<Serializable> jedisBaseDao;

    public EquStatusHandler(PositionProtocal positionProtocal, JedisBaseDao<Serializable> jedisBaseDao) {
        this.positionProtocal = positionProtocal;
        this.jedisBaseDao = jedisBaseDao;
    }

    @Override
    public void exec() {

        alertOthers(positionProtocal,jedisBaseDao);

    }


    /**
     * Created by YINN on 2018/4/20 13:55.
     * @Description : 其他告警
     * @param entry : 协议实体
     * @param jedisBaseDao : redis操作接口
     * @Result : void
     */
    private void alertOthers(PositionProtocal entry, JedisBaseDao jedisBaseDao) {


        //获取二进制字符串
        String binaryStr = CalculationUtils.getAlertInfo(entry.getEquStatus());

        int binaryStrLen = binaryStr.length();

        for (int i = 0; i < binaryStrLen; i++) {
            char item = binaryStr.charAt(i);


            //判断当前字节是否为0（为0则有告警）
            if (ZERO.equals(String.valueOf(item))) {

                //判断告警是否为 本系统可接纳类型
                if (AlertEnum.getAlert(i) == null) {
                    return;
                }

                String alertKey = GetKeysUtils.combineAnyKeys(ALERT, entry.getImei());

                AlertEntry alertEntry = new AlertEntry(i, AlertEnum.getAlert(i).getName(), entry.getRecordTime());

                alertEntry.setImei(entry.getImei());
                alertEntry.setLocType(entry.getLocationType());

                //redis记录告警查询队列
                jedisBaseDao.rpush(alertKey,alertEntry);

                alertEntry.setImei(entry.getImei());
                //redis记录告警推送消息队列
                jedisBaseDao.rpush(ALERT,alertEntry);
            }

        }

    }


}
